import {Col, Form, Input, message, Modal, Row, Spin,Select} from "antd";
import React from "react";
import axios from "../../../../../components/service/request";
import TextArea from "antd/es/input/TextArea";
import './css.less';



const EditModal  = ({ isModalVisible, cancelModal, value, reload, baseUir }) => {

  //确认框的加载状态、旋转加载的状态、模块选项的值和模块选项的更新函数 定义初始状态
    const [confirmLoading, setConfirmLoading] = React.useState(false);
    const [spinLoading, setSpinLoading] = React.useState(false);
    const [title, setTitle] = React.useState("新增信息");
    const [form] = Form.useForm();

    React.useEffect(() => {
        if (isModalVisible) {
            setSpinLoading(true)
            if (value) {
                setTitle("访问信息")
                form.setFieldsValue({
                    id: value.id,
                    type:value.type,
                    director:value.director,
                    content: value.content,
                    unit: value.unit,
                });
            }
            setSpinLoading(false)
        }
    }, [form, isModalVisible, value]);

    //编辑时 保存按钮方法
    const handleOk = () => {
        setConfirmLoading(true)
        form.validateFields()
            .then((values) => {
                form.resetFields();
                axios.post(baseUir+"/save", values).then(res => {
                    if (res.code === 200) {
                        message.info(res.msg)
                        cancelModal()
                        reload()
                    }  else {
                        message.info(res.msg)
                    }
                })
            })
        setConfirmLoading(false)
    }
    //编辑时取消按钮方法
    const handleCancel = () => {
        form.resetFields()
        cancelModal()
    }
    return (
        <Modal title={title}
               visible={isModalVisible}
               onOk={handleOk}
               onCancel={handleCancel}
               okText={'保存'}
               confirmLoading={confirmLoading}
               cancelText={'取消'}
               width={800}   
        >
            <Spin spinning={spinLoading}>
                <Form form={form}
                      labelAlign={'right'}
                      layout={'vertical'}
                >
                    <Form.Item name="id" hidden={true}>
                        <Input/>
                    </Form.Item>
                    <Row gutter={16}>
                        <Col span={12}>
                            <Form.Item name="type" label="访学人员"
                                rules={[
                                    {
                                        required: true,
                                        message: "必须选择类型！"
                                    }]}>
                                 {                                   
                                        <Select
                                        placeholder="请选择人员类型"
                                        style={{width: '100%'}}
                                        allowClear
                                        optionLabelProp="label"
                                    >
                                        <option value="在、访学者" label="在、访学者" >在、访学者</option>
                                        <option value="高级专家" label="高级专家" >高级专家</option>
                                    </Select>                                      
                                 }
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item name="unit" label="单位"
                                rules={[
                                    {
                                        required: true,
                                        message: "必须填写单位！"
                                    }]}>
                                {
                                        <Input/>
                                }
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item name="director" label="负责人"
                                rules={[
                                    {
                                        required: true,
                                        message: "必须填写负责人！"
                                    }]}>
                                {
                                        <Input/>
                                }
                            </Form.Item>
                        </Col>

                    </Row>
                    <Row gutter={16}>
                        <Col span={12}>
                          <Form.Item name="content" label="研究内容"
                              rules={[
                                  {
                                      required: true,
                                      message: "必须填写研究内容！"
                                  }]}>
                              {
                                      <textarea  className="text" ></textarea>
                              }
                          </Form.Item>
                        </Col>
                    </Row>                  
                </Form>
            </Spin>

        </Modal>
    );

}

export default EditModal;
